﻿function InitList() {
    BindCity();
    parkMSys.TryFuncByCondition(function () {
        return $("#ddl_City").val() != null;
    }, function () {
        Search(0);
    });
}

function ajax(method, param, success) {
    $.ajax({
        url: "../Equipment/CollectorList.aspx?m=" + method + "&rnd=" + Math.random()
        , dataType: "json"
        , type: "post"
        , data: param
        , success: function (data) {
            if (!AjaxError(data)) {
                success(data);
            }
        }
    });
};

var pageSize = 20;

function Search(pageIndex) {
    var cid = $("#ddl_City").val();
    var aid = $("#ddl_Area").val();
    var sid = $("#ddl_Street").val();
    var code = encodeURIComponent($("#txtName").val());
    ajax("search", { cid: cid, aid: aid, sid: sid, code: code, pageIndex: pageIndex, pageSize: pageSize }, function (json) {        
        var html = $("#full_CollectorList").tmpl(json);
        $("#tbl_CollectorList").find("tr:not(.td_title)").remove();
        $("#tbl_CollectorList").append(html);

        var pagecount = json.ListCollector.length*2 / pageSize;
        if (json.ListCollector.length % pageSize > 0) {
            pagecount++;
        }
        $("div.pager").pager({ pagenumber: pageIndex, pagecount: pagecount, buttonClickCallback:
                    function (pageclickednumber) {
                        Search(pageclickednumber);
                    }
        });
    });
}

function IsMainCollector(Relationship) {
    if (Relationship.indexOf("主") != -1) return true;
    return false;
}

function TrBtnName(Relationship) {
    if (IsMainCollector(Relationship)) {
        return "设为辅";
    }
    return "设为主";
}

function SetRelation(btn) {
    var tr_me = $(btn).parents("tr");
    var id_me = tr_me.attr("flag3");
    var me_flag1 = tr_me.attr("flag1");
    var me_flag2 = tr_me.attr("flag2");
    var tr_ship = tr_me.parent().find("tr[flag1=" + me_flag1 + "][flag2=" + (3 - me_flag2) + "]");
    var id_ship = tr_ship.attr("flag3");
    
    var td_me = tr_me.find("td:eq(0)");
    var td_ship = tr_ship.find("td:eq(0)");

    var btn_ship = tr_ship.find(":button");
    
    var relation_me = td_me.html();
    var relation_ship = td_ship.html();
    
    var new_me = IsMainCollector(relation_me) ? 0 : 1; 
    var new_ship = IsMainCollector(relation_ship) ? 0 : 1;
    
    ajax("listSetRelation", { id1: id_me, v1: new_me, id2: id_ship, v2: new_ship }, function (json) {
    
        if (json.result == 0) {
            alert("设置失败");
            return;
        }

        if (IsMainCollector(relation_me)) {
            td_me.html("辅收集器");
            $(btn).val("设为主");
        }
        else {
            td_me.html("主收集器");
            $(btn).val("设为辅");
        }


        if (IsMainCollector(relation_ship)) {
            td_ship.html("辅收集器");
            btn_ship.val("设为主");
        }
        else {
            td_ship.html("主收集器");
            btn_ship.val("设为辅");
        }
    });
}

function Edit(id1, id2) {
    $(top.document).find("#ifmFuntion").attr("src", "../Equipment/collectoradd.aspx?id1=" + id1 + "&id2=" + id2);
}

function Delete(id1, id2) {
    if (confirm("确实要删除该组收集器吗?")) {
        ajax("delete", { id1: id1, id2: id2 }, function (json) {
            if (json.result == 1) {
                $("tr[flag3=" + id1 + "],tr[flag3=" + id2 + "]").remove();
            }
            else {
                alert("删除失败");
            }
        });
    }
}

function Location(id1, id2) {
    var html1 = null,html2 = null;
    ajax("getinfo", { id: id1 }, function (json) {
        html1 = {};
        var info = json.CollectorInfo;
        var s = $("#locationTmpl").tmpl(info);
        html1.html = s[0].outerHTML; 
        html1.lng = info.Longitude;
        html1.lat = info.Latitude;
    });
    ajax("getinfo", { id: id2 }, function (json) {
        html2 = {};
        var info = json.CollectorInfo;
        var s = $("#locationTmpl").tmpl(info);
        html2.html = s[0].outerHTML; 
        html2.lng = info.Longitude;
        html2.lat = info.Latitude;
    });
    parkMSys.TryFuncByCondition(function () {
        return html1 && html2;
    }, function () {
        MapLocation([html1, html2]);
    });
}

function InitAdd() {
    BindCity();
    var add = QueryString("add") != null;
    if (!add) {
        $("#title").text("修改收集器");
        $("#type").val("edit");
        var id1 = QueryString("id1");
        var id2 = QueryString("id2");
        $("#id1").val(id1);
        $("#id2").val(id2);

        ajax("getinfo", { id: id1 }, function (json) {
            var info = json.CollectorInfo;
            $("#code1").val(info.Code);
            $("#lng1").val(info.Longitude);
            $("#lat1").val(info.Latitude);
            $("#conn1").val(info.Connection);
            if (info.Status == "启用") {
                $("#chk1").attr("checked", "checked");
            }
            else {
                $("#chk1").removeAttr("checked");
            }
            if (IsMainCollector(info.Relationship)) {
                $("#rad1").attr("checked", "checked");
            }
        });

        ajax("getinfo", { id: id2 }, function (json) {
            var info = json.CollectorInfo;
            $("#code2").val(info.Code);
            $("#lng2").val(info.Longitude);
            $("#lat2").val(info.Latitude);
            $("#conn2").val(info.Connection);
            if (info.Status == "启用") {
                $("#chk2").attr("checked", "checked");
            }
            else {
                $("#chk2").removeAttr("checked");
            }
            if (IsMainCollector(info.Relationship)) {
                $("#rad2").attr("checked", "checked");
            }

            $("#ddl_City").val(info.cityId);
            $("#ddl_Area").val(info.areaId);
            $("#ddl_Street").val(info.streetId);
        });

        $("#btnSave").click(function () {
            var input = {};
            input.CollectorInfo = [];
            var obj = {};
            obj.cityId = $("#ddl_City").val();
            obj.areaId = $("#ddl_Area").val();
            obj.streetId = $("#ddl_Street").val();
            obj.Code = $("#code1").val();
            obj.Longitude = $("#lng1").val();
            obj.Latitude = $("#lat1").val();
            obj.Connection = $("#conn1").val();
            if ($("#rad1").is(":checked")) {
                obj.Relationship = "主收集器";
            }
            else {
                obj.Relationship = "辅收集器";
            }
            if ($("#chk1").is(":checked")) {
                obj.Status = "启用";
            }
            else {
                obj.Status = "未启用";
            }
            input.CollectorInfo.push(obj);

            obj = {};
            //            obj.id = $("#id2").val();
            obj.Code = $("#code2").val();
            obj.Longitude = $("#lng2").val();
            obj.Latitude = $("#lat2").val();
            obj.Connection = $("#conn2").val();
            if ($("#rad2").is(":checked")) {
                obj.Relationship = "主收集器";
            }
            else {
                obj.Relationship = "辅收集器";
            }
            if ($("#chk2").is(":checked")) {
                obj.Status = "启用";
            }
            else {
                obj.Status = "未启用";
            }
            input.CollectorInfo.push(obj);

            input = JSON.stringify(input);

            ajax("edit", { info: encodeURIComponent(input) }, function (json) {
                if (json.result == 1) {
                    alert("修改成功");
                    window.location.replace("../Equipment/collectorlist.aspx");
                }
                else {
                    alert("修改失败");
                }
            });
        });
    }
    else {
        //$("#trConnection").hide();
        $("#btnSave").click(function () {
            var input = {};
            input.CollectorInfo = [];
            var obj = {};
            obj.cityId = $("#ddl_City").val();
            obj.areaId = $("#ddl_Area").val();
            obj.streetId = $("#ddl_Street").val();
            obj.id = $("#id1").val();
            obj.Code = $("#code1").val();
            obj.Longitude = $("#lng1").val();
            obj.Latitude = $("#lat1").val();
            obj.Connection = $("#conn1").val();
            if ($("#rad1").is(":checked")) {
                obj.Relationship = "主收集器";
            }
            else {
                obj.Relationship = "辅收集器";
            }
            if ($("#chk1").is(":checked")) {
                obj.Status = "启用";
            }
            else {
                obj.Status = "未启用";
            }
            input.CollectorInfo.push(obj);

            obj = {};
            obj.id = $("#id2").val();
            obj.Code = $("#code2").val();
            obj.Longitude = $("#lng2").val();
            obj.Latitude = $("#lat2").val();
            obj.Connection = $("#conn2").val();
            if ($("#rad2").is(":checked")) {
                obj.Relationship = "主收集器";
            }
            else {
                obj.Relationship = "辅收集器";
            }
            if ($("#chk2").is(":checked")) {
                obj.Status = "启用";
            }
            else {
                obj.Status = "未启用";
            }
            input.CollectorInfo.push(obj);

            input = JSON.stringify(input);

            ajax("add", { info: encodeURIComponent(input) }, function (json) {
                if (json.result == 1) {
                    //$("#code1,#code2,#lng1,#lng2,#lat1,#lat2").val("");
                    alert("添加成功");
                    window.location.replace("../Equipment/collectorlist.aspx");
                }
                else {
                    alert("添加失败");
                }
            });
        });
    }

}

